/**
 * $Id: PMTileMapper.as 67 2006-08-13 15:49:35Z miller $
 *
 *  Included by PMTileMapper.mxml
 */

import mx.controls.scrollClasses.*;
import mx.managers.PopUpManager;
import mx.containers.TitleWindow;
import mx.events.*;

private var viewWidth : int, viewHeight : int;
internal var bDragging : Boolean;
internal var nShortStatusWidth : int;

internal var xmlMapData : XML;

public function initApp()  : void {
	map.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
//				addEventListener(MouseEvent.MOUSE_OUT, onMouseUp);
	map.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
	map.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
	
	addEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
	addEventListener( KeyboardEvent.KEY_DOWN, onKeyDown );
	addEventListener( KeyboardEvent.KEY_UP,   onKeyUp   );
	
	stage.focus = this
//				trace( 'Stage ', stage );

	nShortStatusWidth = 60
//						get( "javascript:alert('ok!');" );
//						ExternalInterface.call( "alert", [ "external" ] );
	
	//ExternalInterface.call( "file_open", [ "external" ] );
			
	
	loadMap( 'res/test.map.xml', 'map1' );	
}			

public function loadMap( url : String, strMapID : String = null )  : void {
	var loader:URLLoader = new URLLoader();
	var request:URLRequest = new URLRequest( url );
	loader.load(request);
	loader.addEventListener(Event.COMPLETE, onLoadMapComplete);
	
	function onLoadMapComplete( event: Event ) : void {
		var loader:URLLoader = URLLoader(event.target);
		var mapXML : XML = new XML(loader.data);
		
//		if( strMapID ) {
//			mapXML = mapXML.map.(@id == strMapID)[0]; 
//		}
		
		loadXMLMapData( mapXML );
	}
	

	
//				map.init( 37, 45, 32, 32 );
//				map.setTileSet( "res/tiles.png" );
	
//				setScrollBars();
//				updateScroll();				
}

public function loadXMLMapData( mapXML : XML ) : Boolean {
	xmlMapData = mapXML;
	var strMapID : String = "map1";
	mapXML = mapXML.map.(@id == strMapID)[0]; 
	return map.loadXMLMapData( mapXML );
}

public function setScrollBars() : void {
	scrollV.setScrollProperties( 50, 0, map.worldHeight - map.height / map.getScale(), 50 );
	scrollH.setScrollProperties( 50, 0, map.worldWidth - map.width / map.getScale(), 50 );				
}

internal var mode : int = HAND, lastMode : int = -1;			
internal static const ARROW : int = 0;
internal static const HAND : int = 1;

public function onKeyDown( evt: KeyboardEvent ) : void {
	trace( 'keyDown '+evt.toString() )
	var c : String = String.fromCharCode( evt.charCode );

	switch( c ) {
		case 'a':
			setMode( ARROW );
			break;
		case 'v': 						
		case ' ':
			if( mode != HAND && c == ' ' )
				lastMode = mode;
			setMode( HAND );
			break;
	}				
}

public function setMode( nmode : int ) : void {
	mode = nmode;
	
	this.useHandCursor = false;
	switch( mode ) {
		case HAND:
			this.useHandCursor = true;
			break;					
	}
}

public function onKeyUp( evt: KeyboardEvent ) : void {
	trace( 'keyUp '+evt.toString() )
	if( lastMode > -1 ) {
		mode = lastMode;
		lastMode = -1;
	}
}

public function onMouseUp( evt : MouseEvent ) : void {
	switch( mode ) {
		case HAND:
			bDragging = false;
			break;
	}
}

public function setScale( scale : Number ) : void {
	map.setScale( scale );
	setScrollBars();				
	setStatusText( "Scale: "+map.getScale() );
}

public function onMouseWheel( evt : MouseEvent ) : void {
	setScale( map.getScale() * ( 1 + 0.05 * evt.delta ));
}

public function setStatusText( str : String ) : void {
	status3.text = str;
}

public function onMouseMove( evt: MouseEvent ) : void {
	var row : int = map.pickRow( evt.localY );
	var col : int = map.pickCol( evt.localX );
	status1.text = "( "+col+", "+row+" )"
	status2.text = "Tile "+map.getCell( col, row )
	
	status1.width = nShortStatusWidth
	status2.width = nShortStatusWidth				
					
	switch( mode ) {
		case HAND:
			if( !bDragging )
				return;
			scrollH.scrollPosition -= evt.localX - lastMouseX;
			scrollV.scrollPosition -= evt.localY - lastMouseY;
			lastMouseX = evt.localX 
			lastMouseY = evt.localY
			
			updateScroll();
			break;
	}
}

internal var lastMouseX : int, lastMouseY : int;

public function onMouseDown( evt : MouseEvent ) : void {
	switch( mode ) {
		case HAND:
			bDragging = true;
			lastMouseX = evt.localX 
			lastMouseY = evt.localY
			break;
	}
}



public function updateScroll() : void  {	
	trace( 'Stage ', stage );
				
	scrollH.scrollPosition = Math.min( scrollH.maxScrollPosition, Math.max( scrollH.scrollPosition, 0 ))
	scrollV.scrollPosition = Math.min( scrollV.maxScrollPosition, Math.max( scrollV.scrollPosition, 0 ))
	map.setViewWindow( scrollH.scrollPosition, scrollV.scrollPosition, map.width, map.height );
}

private function chooseOpenMapFile() : void {
	var fr : FileReference = new FileReference()
	var filterMap : FileFilter = new FileFilter( "Map files (*.map.xml)", "*.map.xml;" );				
	fr.addEventListener( Event.SELECT, onFileSelect );
	
	function onFileSelect( evt : Event ) : void {
		var file:FileReference = FileReference(evt.target);
		trace( 'selected '+file.name )
	}
	
	fr.browse( [ filterMap ] );
}

private function openDlgMapData() : void {
	var dlgFile : DlgMapData = DlgMapData(PopUpManager.createPopUp( this, DlgMapData, true ));
	dlgFile.visible = false;
	dlgFile.title = "XML Map Data";
	dlgFile.showCloseButton=true;
	dlgFile.target = this;
	dlgFile.setData( xmlMapData );
	PopUpManager.centerPopUp( dlgFile );
	dlgFile.visible = true;
}

private function handlerMenu( evt : MenuEvent ) : void {
	trace( "menu event "+[evt.item, evt.item.@data, evt.item.@label] );
	var cmd : String = evt.item.@data;				
	switch( cmd ) {
		case "zoom_actual":
			setScale( 1 );
			break;
		case "file_new":
			break;			
		case "file_mapdata":
			openDlgMapData();
			break;
		case "file_open":
			chooseOpenMapFile();
			break;
	}
}
